/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import java.io.IOException;

/**
 * Represents the &lt;link /&gt; tag. 
 * Does not implement a full W3C spec due to the fact that while a link might have events, 
 * there is no way to fire them.
 * @author J Patrick Davenport
 *
 */
public class Link extends ChildlessPageElement implements ILanguageAttributes<Link>, ICoreAttributes {
	private static final String	LINK_TAG		= "link";
	private static final String	CHARSET_KEY		= "charset";
	private static final String	HREF_KEY		= "href";
	private static final String	HREF_LANG_KEY	= "hreflang";
	private static final String	MEDIA_KEY		= "media";
	private static final String	REL_KEY			= "rel";
	private static final String	REV_KEY			= "rev";
	private static final String	TYPE_KEY		= "type";
	private boolean				autoAppendContext;

	{
		this.setSupportsAttributes(true);
		this.setTag(LINK_TAG);
	}

	public Link() {

	}

	/**
	 * Flags that Link to append the context to the HREF attribute.
	 * 
	 * @param _autoAppendContext if true, the context will be added to the base href.
	 * For example href="/mysite.css" would be come href="/AContext/mysite.css". <strong>Note:</strong> You must provide the begining "/".
	 */
	public Link(final boolean _autoAppendContext) {
		this.autoAppendContext = _autoAppendContext;
	}

	public String getType() {
		return this.getAttributeValue(TYPE_KEY);
	}

	public Link setType(final String _value) {
		return (Link) this.setAttribute(TYPE_KEY, _value);
	}

	public String getRev() {
		return this.getAttributeValue(REV_KEY);
	}

	public Link setRev(final String _value) {
		return (Link) this.setAttribute(REV_KEY, _value);
	}

	public String getRel() {
		return this.getAttributeValue(REL_KEY);
	}

	public Link setRel(final String _value) {
		return (Link) this.setAttribute(REL_KEY, _value);
	}

	public String getMedia() {
		return this.getAttributeValue(MEDIA_KEY);
	}

	public Link setMedia(final String _value) {
		return (Link) this.setAttribute(MEDIA_KEY, _value);
	}

	public String getHrefLang() {
		return this.getAttributeValue(HREF_LANG_KEY);
	}

	public Link setHrefLang(final String _value) {
		return (Link) this.setAttribute(HREF_LANG_KEY, _value);
	}

	public String getHref() {
		return this.getAttributeValue(HREF_KEY);
	}

	public Link setHref(final String _value) {
		return (Link) this.setAttribute(HREF_KEY, _value);
	}

	public String getCharset() {
		return this.getAttributeValue(CHARSET_KEY);
	}

	public Link setCharset(final String _value) {
		return (Link) this.setAttribute(CHARSET_KEY, _value);
	}

	@Override
	public String getCssClass() {
		return this.getAttributeValue(CSS_CLASS_KEY);
	}

	@Override
	public String getId() {
		return this.getAttributeValue(ID_KEY);
	}

	@Override
	public String getStyle() {
		return this.getAttributeValue(STYLE_KEY);
	}

	@Override
	public String getTitle() {
		return this.getAttributeValue(TITLE_KEY);
	}

	@Override
	public Link setCssClass(final String _value) {
		return (Link) this.setAttribute(CSS_CLASS_KEY, _value);
	}

	@Override
	public Link setId(final String _value) {
		return (Link) this.setAttribute(ID_KEY, _value);
	}

	@Override
	public Link setStyle(final String _value) {
		return (Link) this.setAttribute(STYLE_KEY, _value);
	}

	@Override
	public Link setTitle(final String _value) {
		return (Link) this.setAttribute(TITLE_KEY, _value);
	}

	@Override
	public Link setDir(final String _value) {
		return (Link) this.setAttribute(DIR_KEY, _value);
	}

	@Override
	public Link setLang(final String _lang) {
		return (Link) this.setAttribute(LANG_KEY, _lang);
	}

	@Override
	public Link setXmlLang(final String _xmlLang) {
		return (Link) this.setAttribute(XML_LANG_KEY, _xmlLang);
	}

	@Override
	public String getDir() {
		return this.getAttributeValue(DIR_KEY);
	}

	@Override
	public String getLang() {
		return this.getAttributeValue(LANG_KEY);
	}

	@Override
	public String getXmlLang() {
		return this.getAttributeValue(XML_LANG_KEY);
	}

	public boolean isAutoAppendContext() {
		return this.autoAppendContext;
	}

	public Link setAutoAppendContext(final boolean _autoAppendContext) {
		this.autoAppendContext = _autoAppendContext;
		return this;
	}

	@Override
	public void render() throws IllegalStateException, IOException {
		if (this.autoAppendContext) {
			this.setAttribute(HREF_KEY, this.getContextPath() + "/" + this.getAttributeValue(HREF_KEY));
		}
		super.render();
	}
}
